﻿using ApiLib.Cloud.Common;
using ApiLib.Cloud.Models;
using ApiLib.Controllers;
using ApiLib.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ApiLib.Cloud.Controllers;

/// <summary>
/// 云服务
/// </summary>
[ApiDescriptionSettings(MyCloudConstants.ApiDescriptionSettings)]
public class CloudController : BaseController
{
    /// <summary>
    /// 
    /// </summary>
    public CloudController()
    {
        CloudUtil.InitConfig();
    }
    
    /// <summary>
    /// 健康检查。
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpGet]
    public string Health()
    {
        /*if (MyCloudConstants.IsZuul)
        {
            var host = Request.Host.Host;
            var serviceInfo = ServiceInfos.Find(info => info.Host == host);
            if (serviceInfo == null)
            {
                throw ActionException("未注册");
            }

            serviceInfo.HealthTime = LocalTime;
        }*/

        return "ok";
    }

    /// <summary>
    /// 注册
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    [AllowAnonymous]
    public string Register(ServiceConfig config)
    {
        if (!MyCloudConstants.IsZuul)
        {
            throw ActionException("非法行为");
        }

        if (!config.Key.Equals(MyCloudConstants.Config.Key))
        {
            throw ActionException("非法服务");
        }

        // var iPv4 = HttpContext.GetRemoteIpAddressToIPv4();
        // var host = Request.Scheme + "://" + Request.Host.Value;
        var serviceInfo = Db.Queryable<ServiceInfo>().First(info => info.Name == config.Name && info.Host==config.Host);
        if (serviceInfo == null)
        {
            serviceInfo = new ServiceInfo
            {
                Host = config.Host,
                Key = config.Key,
                Name = config.Name,
                Path = config.Path,
                Online = true,
                Type = 0,
                Description = config.Description,
                HealthTime = LocalTime,
                RegisterTime = LocalTime
            };
            Db.Insertable(serviceInfo).ExecuteCommand();
            serviceInfo.AddRegisterLog();
        }
        else
        {
            serviceInfo.RegisterTime = LocalTime;
            serviceInfo.Online = true;
            serviceInfo.RetryCount = 0;
            Db.Updateable(serviceInfo).UpdateColumns(info => new{info.RegisterTime,info.Online,info.RetryCount}).ExecuteCommand();
        }

        CloudUtil.GetServiceInfo(true);
        return "ok";
    }

    /// <summary>
    /// 所有服务列表
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    public List<ServiceInfo>? ServiceInfoList(bool? online = null)
    {
        if (!MyCloudConstants.IsZuul)
        {
            throw ActionException("非法行为");
        }

        var serviceInfos = CloudUtil.GetServiceInfo();
        if (online!=null)
        {
            return serviceInfos.Where(info => info.Online == online).ToList();
        }

        return serviceInfos;
    }
    /// <summary>
    /// 所有服务列表
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    public List<SysCloudRoute>? RouteInfoList()
    {
        if (!MyCloudConstants.IsZuul)
        {
            throw ActionException("非法行为");
        }

        return CloudUtil.GetRoute();
    }
}